package com.hxalex.simulationhandle.ui.task;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.hxalex.simulationhandle.R;
import com.hxalex.simulationhandle.databinding.ActivityAddTaskBinding;
import com.hxalex.simulationhandle.ui.base.BaseActivity;
import com.hxalex.simulationhandle.ui.home.model.KeyWordBean;
import com.hxalex.simulationhandle.ui.home.model.TaskBean;
import com.hxalex.simulationhandle.ui.home.model.WebSiteBean;
import com.hxalex.simulationhandle.ui.home.presenter.TaskPersenter;
import com.leon.lfilepickerlibrary.LFilePicker;

import java.util.ArrayList;
import java.util.List;

public class AddTaskActivity extends BaseActivity {
    private static final int REQUESTCODE_FROM_ACTIVITY_KEYWORD=1000;
    private static final int REQUESTCODE_FROM_ACTIVITY_WEBSITE=1001;
 private ActivityAddTaskBinding mBinding;
 private List<KeyWordBean> keyWordBeans;
    private  List<WebSiteBean> webSiteBeans;
    private TaskPersenter mPersenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding= DataBindingUtil.setContentView(this,R.layout.activity_add_task);
        intiView();
        initToolBarWithRightMenu("添加", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addTask();
            }
        });
     
    }

    private void intiView() {
        mPersenter=new TaskPersenter(this);
        mBinding.btnAddKeyWord.setOnClickListener(v->
        {
            openFileList(REQUESTCODE_FROM_ACTIVITY_KEYWORD);
        });
        mBinding.btnAddWebSite.setOnClickListener(v->
        {
            openFileList(REQUESTCODE_FROM_ACTIVITY_WEBSITE);
        });
    }
    
    
    private void openFileList(int code)
    {
        new LFilePicker().withActivity(this)
                .withRequestCode(code)
                .withStartPath("/storage/emulated/0")
                .withIsGreater(false)
                .withFileSize(500 * 1024)
                .withFileFilter(new String[]{".txt"})
                .withMutilyMode(false)
                .withBackgroundColor("#2BD491")
                .start();
    }


    private void addTask() {
        String errormsg="";
        String txtTaskTitle = mBinding.txtTaskTitle.getText().toString();
        String txtExecute = mBinding.txtExecute.getText().toString();
        String txStopTime = mBinding.txtExecute.getText().toString();
        if(txtTaskTitle.equals("") || txtTaskTitle==null) errormsg="标题不能为空";
        if(txtExecute.equals("") || txtExecute==null) errormsg="执行次数不能为空";
        if(keyWordBeans==null || keyWordBeans.size()==0) errormsg="词库内容不能为空";
        if(webSiteBeans==null || webSiteBeans.size()==0) errormsg="网站库内容不能为空";
        if(txStopTime.equals("") || txStopTime==null) errormsg="页面停留时间不能为空";
        if(!errormsg.equals(""))
        {
            Toast.makeText(this,errormsg,Toast.LENGTH_SHORT).show();
            return;
        }
        TaskBean taskBean=new TaskBean(txtTaskTitle,"未执行",keyWordBeans.size(),webSiteBeans.size(),Integer.valueOf(txtExecute),Integer.valueOf(txStopTime));
        if(mPersenter.addTask(taskBean, keyWordBeans, webSiteBeans))
        {
            Toast.makeText(this,"添加成功",Toast.LENGTH_LONG).show();
            finish();
        }   

    }
    
    private void loadkeyWord(String path)
    {
        List<String> strings = mPersenter.parseKeyWordFile(path);
        keyWordBeans=new ArrayList<>();
        for(String s:strings)
        {
            keyWordBeans.add(new KeyWordBean(s));
        }
        
        if(keyWordBeans.size()>0)
        {
            Toast.makeText(this,"网站库解析完成，总共"+keyWordBeans.size()+"个网站库词",Toast.LENGTH_LONG).show();
            mBinding.txtKeywordHint.setHint(keyWordBeans.size()+"个关键词");
        }
        
        for(KeyWordBean b:keyWordBeans) Log.i("msg",b.getKeyWordString() );
      
    }

    private void loadWebSite(String path)
    {
        List<String> strings = mPersenter.parseKeyWordFile(path);
        webSiteBeans=new ArrayList<>();
        for(String s:strings)
        {
            webSiteBeans.add(new WebSiteBean(s));
        }

        if(webSiteBeans.size()>0)
        {
            Toast.makeText(this,"网站库解析完成，总共"+webSiteBeans.size()+"个网站库词",Toast.LENGTH_LONG).show();
            mBinding.txtWebsiteHint.setHint(webSiteBeans.size()+"个网站库词");
        }
        
        for(WebSiteBean b:webSiteBeans) Log.i("msg",b.getWebSiteString() );
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUESTCODE_FROM_ACTIVITY_KEYWORD|| requestCode ==REQUESTCODE_FROM_ACTIVITY_WEBSITE) {
                ArrayList<String> path =data.getStringArrayListExtra("paths");
                Log.i("msg", path.get(0)+"   size:"+path.size());
                if(path.size()<0) return;
                if(requestCode == REQUESTCODE_FROM_ACTIVITY_KEYWORD) loadkeyWord(path.get(0));
                if(requestCode == REQUESTCODE_FROM_ACTIVITY_WEBSITE) loadWebSite(path.get(0));
            }
        }
        
    }
}
